package org.automated.realestatemanager.db.entity;

import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "advertisements")
public class Advertisement extends HasId implements Comparable<Advertisement> {

	@Id
	@Column(name = "adv_id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Column(name = "id_from_site")
	private Long idFromSite;

	@Column(length = 50)
	private String agency;

	@Column(length = 500)
	private String text;

	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "adv_phones")
	private Set<Phone> phones = new TreeSet<Phone>();

	@ManyToOne
	@JoinColumn(name = "publisher_id")
	private Publisher publisher;

	@ManyToOne
	@JoinColumn(name = "category_id")
	private Category category;

	@Column(length = 20)
	private String rooms = "";

	@Column(length = 20)
	private String floor = "";

	@Column(length = 20)
	private String area = "";

	@Column(name = "web_site", length = 50)
	private String webSite = "";

	@Column(length = 20)
	private String sourse = "";

	@Column(length = 10)
	private String price = "";

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public void setId(final Long id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(final String text) {
		this.text = text;
	}

	public Set<Phone> getPhones() {
		return phones;
	}

	public void setPhones(final Set<Phone> phones) {
		this.phones = phones;
	}

	public Publisher getPublisher() {
		return publisher;
	}

	public void setPublisher(final Publisher publisher) {
		this.publisher = publisher;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(final Category category) {
		this.category = category;
	}

	public String getAgency() {
		return agency;
	}

	public void setAgency(final String agency) {
		this.agency = agency;
	}

	public String getRooms() {
		return rooms;
	}

	public void setRooms(final String rooms) {
		this.rooms = rooms;
	}

	public String getFloor() {
		return floor;
	}

	public void setFloor(final String floor) {
		this.floor = floor;
	}

	public String getSourse() {
		return sourse;
	}

	public void setSourse(String sourse) {
		this.sourse = sourse;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public Long getIdFromSite() {
		return idFromSite;
	}

	public void setIdFromSite(Long idFromSite) {
		this.idFromSite = idFromSite;
	}

	public String getWebSite() {
		return webSite;
	}

	public void setWebSite(String webSite) {
		this.webSite = webSite;
	}

	public int compareTo(Advertisement o) {
		System.out.println("Compare advs:\n" + text + "\n and \n" + o.getText()
				+ "\n Result: \n" + text.compareTo(o.getText()));
		return text.compareTo(o.getText());
	}
}
